<?php

namespace app\health\job;

use app\health\model\HealthUserTask;
use app\health\model\HealthWebsiteTask;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\queue\Job;

class WebsiteTaskJob extends BaseJob
{
    /**
     * 完成系统任务
     * @param Job $job
     * @param $data
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function completeSystem(Job $job, $data): void
    {
        $job->delete();
        $completeWay    = $data['complete_way'] ?? 0;
        $userId         = $data['user_id'] ?? 0;
        $completeWayMap = [2, 3, 4, 5];
        if (in_array($completeWay, $completeWayMap) && $userId > 0) {
            $task = HealthWebsiteTask::where([
                'complete_way' => $completeWay,
                'status'       => 1,
                'online'       => 1,
            ])->find();
            if ($task && HealthUserTask::where('user_id', $userId)->where('task_id', $task['id'])->where('status', 1)->count() === 0) {
                HealthUserTask::create([
                    'task_id'           => $task['id'],
                    'task_category'     => $task['category'],
                    'user_id'           => $userId,
                    'status'            => 1,
                    'receive'           => 0,
                    'award_type'        => $task['award_type'],
                    'award_value'       => $task['award_value'],
                    'task_complete_num' => 1,
                    'complete_num'      => 1,
                    'complete_time'     => time(),
                ]);
            }
        }
    }
}